﻿using BepInEx;
using BepInEx.Configuration;
using CheatMenu.Patch;
using CheatMenu.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace CheatMenu.Managers
{
    public class GUIManager
    {
        //Constants
        public const float ScrollBarWidth = 16f;
        public const float TitleBarHeight = 10f;
        public const float PaddingSize = 5f;
        public const int DefaultProposedItemCount = 40;
        public const int MinProposedItemCount = 20;
        public const int MaxProposedItemCount = 60;
        public const int DefaultMinControlHeight = 15;
        public const int MinMinControlHeight = 10;
        public const int MaxMinControlHeight = 30;
        public const bool DefaultInstantText = false;
        public const bool DefaultInstantWait = false;
        public const bool DefaultInstantFade = false;
        public const bool DefaultAsyncLoadScene = false;
        public const bool DefaultRunInBackground = false;
        public const bool DefaultAutoCloseLoadScene = true;

        public static int ControlHeight { get; private set; }
        public static int ControlWidth { get; private set; }
        public static int ControlWidthTimesTwo { get; private set; }
        public static int ControlFontSize { get; private set; }
        public static int TopPadding { get; private set; }
        public static Color LabelHeaderColor { get; set; } = new Color(0.9f, 0.3f, 0.23f);
        public static Color LabelColor { get; set; } = Color.white;
        public static Color SelectedButtonColor { get; set; } = Color.green;

        private static ConfigEntry<int> configMinControlHeight;
        public static int MinControlHeight
        {
            get => configMinControlHeight.Value;
            set => configMinControlHeight.Value = value;
        }
        private static ConfigEntry<int> configProposedItemCount;
        public static int ProposedItemCount
        {
            get => configProposedItemCount.Value;
            set => configProposedItemCount.Value = value;
        }
        private static ConfigEntry<bool> configInstantText;
        public static bool InstantText
        {
            get => configInstantText.Value;
            set => configInstantText.Value = value;
        }

        private static ConfigEntry<bool> configInstantWait;
        public static bool InstantWait
        {
            get => configInstantWait.Value;
            set => configInstantWait.Value = value;
        }

        private static ConfigEntry<bool> configInstantFade;
        public static bool InstantFade
        {
            get => configInstantFade.Value;
            set => configInstantFade.Value = value;
        }

        private static ConfigEntry<bool> configAsyncLoadScene;
        public static bool AsyncLoadScene
        {
            get => configAsyncLoadScene.Value;
            set => configAsyncLoadScene.Value = value;
        }

        private static ConfigEntry<bool> configRunInBackground;
        public static bool RunInBackground
        {
            get => configRunInBackground.Value;
            set => configRunInBackground.Value = value;
        }

        private static ConfigEntry<bool> configAutoCloseLoadScene;
        public static bool AutoCloseLoadScene
        {
            get => configAutoCloseLoadScene.Value;
            set => configAutoCloseLoadScene.Value = value;
        }

        public static void Initialize(CheatMenuPlugin plugin)
        {
            
            configMinControlHeight = plugin.Config.Bind("GUI", "MinControlHeight", DefaultMinControlHeight);
            if (MinControlHeight < MinMinControlHeight || MinControlHeight > MaxMinControlHeight)
                MinControlHeight = Mathf.Clamp(MinControlHeight, MinMinControlHeight, MaxMinControlHeight);
            configProposedItemCount = plugin.Config.Bind("GUI", "ProposedItemCount", DefaultProposedItemCount);
            if (ProposedItemCount < MinProposedItemCount || ProposedItemCount > MaxProposedItemCount)
                ProposedItemCount = Mathf.Clamp(ProposedItemCount, MinProposedItemCount, MaxProposedItemCount);
            configAutoCloseLoadScene = plugin.Config.Bind("GUI", "AutoCloseLoadScene", DefaultAutoCloseLoadScene);
            configInstantText = plugin.Config.Bind("Game", "InstantText", DefaultInstantText);
            configInstantWait = plugin.Config.Bind("Game", "InstantWait", DefaultInstantWait);
            configInstantFade = plugin.Config.Bind("Game", "InstantFade", DefaultInstantFade);
            configAsyncLoadScene = plugin.Config.Bind("Game", "AsyncLoadScene", DefaultAsyncLoadScene);
            InstantTextPatcher.Enabled = InstantText;
            InstantWaitPatcher.Enabled = InstantWait;
            InstantFadePatcher.Enabled = InstantFade;
            AsyncLoadScenePatcher.Enabled = AsyncLoadScene;
            configRunInBackground = plugin.Config.Bind("Game", "RunInBackground", DefaultRunInBackground);
            Application.runInBackground = RunInBackground;

            CalculateScaling();
        }

        public static void CalculateScaling()
        {
            float proposedButtonHeight = (float)Screen.height / ProposedItemCount;
            proposedButtonHeight = Math.Max(MinControlHeight, proposedButtonHeight);
            ControlHeight = (int)proposedButtonHeight;
            ControlWidth = (int)(proposedButtonHeight * 6f);
            ControlWidthTimesTwo = (int)(proposedButtonHeight * 12f);
            ControlFontSize = (int)(proposedButtonHeight * 0.75f);
            TopPadding = (int)(Screen.height * 0.075f);
        }

        public static Rect GetWorkingArea() => new Rect(0, TopPadding, Screen.width, Screen.height - TopPadding);
    }
}
